//
//  B_AddGoodsDetailVC.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/9/3.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_AddGoodsDetailVC.h"
#import "B_AddGoodsTVC.h"
#import "B_AddGoodsLabelTVC.h"
#import "B_AddGoodsImageViewTVC.h"
#import "B_AddGoodsModel.h"
#import "B_InputTextVC.h"
#import "XGPhotoManager.h"


@interface B_AddGoodsDetailVC ()

@property (nonatomic, strong) BaseView * mainView;

@property (nonatomic, strong) XGPhotoManager * photoManager;

@end

@implementation B_AddGoodsDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    WeakSelf(weSelf, self);
    [self setTitle:@"商品详情"];
    if (self.B_array.count == 0) {
        self.dataArray = [NSMutableArray array];
    } else {
        self.dataArray = [NSMutableArray arrayWithArray:self.B_array];
    }
    self.view = self.mainView;
    [self setRightTextBarButtonItemWithFrame:CGRectZero title:@"完成" titleColor:[UIColor color_pink] backImage:@"" selectBackImage:@"" action:^(GUIButton *button) {
        if (weSelf.B_addGods) {
            weSelf.B_addGods(weSelf.dataArray);
        }
        [weSelf.navigationController popViewControllerAnimated:YES];
    }];
}

- (BaseView *)mainView {
    if (!_mainView) {
        WeakSelf(weSelf, self);
        _mainView = [[BaseView alloc] init];
        [_mainView setTableViewFrameWithTop:5 left:0 right:0 bottom:0];
        _mainView.numberOfRowsBlock = ^NSInteger(UITableView *tableView, NSInteger section) {
            return self.dataArray.count + 1;
        };
        [_mainView.tableView registerNib:[UINib nibWithNibName:@"B_AddGoodsTVC" bundle:nil] forCellReuseIdentifier:@"B_AddGoodsTVC"];
        [_mainView.tableView registerNib:[UINib nibWithNibName:@"B_AddGoodsLabelTVC" bundle:nil] forCellReuseIdentifier:@"B_AddGoodsLabelTVC"];
        [_mainView.tableView registerNib:[UINib nibWithNibName:@"B_AddGoodsImageViewTVC" bundle:nil] forCellReuseIdentifier:@"B_AddGoodsImageViewTVC"];
        _mainView.cellForRowBlock = ^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath) {
            if (self.dataArray.count == indexPath.row) {
                B_AddGoodsTVC * cell = [tableView dequeueReusableCellWithIdentifier:@"B_AddGoodsTVC" forIndexPath:indexPath];
                cell.B_addGoodsAction = ^{
                    //TODO:执行点击事件
                    
                    [weSelf alertViewWithTitle:@"请选择" message:@"" buttonsTitles:@[@"添加文字", @"相机", @"从相册选择", @"取消"] action:^(NSInteger index) {
                        
                        if (index == 0) {
                            
                            B_InputTextVC * input = [[B_InputTextVC alloc] init];
                            input.B_doneAction = ^(NSString *string) {
                                B_AddGoodsModel * model = [[B_AddGoodsModel alloc] init];
                                model.B_text = string;
                                [weSelf.dataArray addObject:model];
                                [tableView reloadData];
                            };
                            [weSelf pushNextViewController:input];
                        } else if (index == 1) {
                            [weSelf.photoManager openCamereWithController:weSelf withImage:^(UIImage *image) {
                                B_AddGoodsModel * model = [[B_AddGoodsModel alloc] init];
                                model.B_image = image;
                                [weSelf.dataArray addObject:model];
                                [tableView reloadData];
                            }];
                        } else if (index == 2) {
                            [weSelf.photoManager openSinglePhotoWithController:weSelf withImage:^(UIImage *image) {
                                B_AddGoodsModel * model = [[B_AddGoodsModel alloc] init];
                                model.B_image = image;
                                [weSelf.dataArray addObject:model];
                                [tableView reloadData];
                            }];
                        }
                    }];
                    
                    
                    
                };
                return cell;
            } else {
                B_AddGoodsModel * model = weSelf.dataArray[indexPath.row];
                if (ISNULL(model.B_text)) {
                    B_AddGoodsImageViewTVC * cell = [tableView dequeueReusableCellWithIdentifier:@"B_AddGoodsImageViewTVC" forIndexPath:indexPath];
                    cell.B_imageView.image = model.B_image;
                    return cell;
                } else {
                    B_AddGoodsLabelTVC * cell = [tableView dequeueReusableCellWithIdentifier:@"B_AddGoodsLabelTVC" forIndexPath:indexPath];
                    
                    cell.B_label.text = model.B_text;
                    return cell;
                }
            }
        };
        
        _mainView.didSelectRow = ^(UITableView *tableView, NSIndexPath *indexPath) {
            if (self.dataArray.count == indexPath.row) {
                
            } else {
                B_AddGoodsModel * model = weSelf.dataArray[indexPath.row];
                if (ISNULL(model.B_text)) {
                    [weSelf alertViewWithTitle:@"请选择" message:@"" buttonsTitles:@[@"相机", @"从相册选择", @"取消"] action:^(NSInteger index) {
                        if (index == 0) {
                            [weSelf.photoManager openCamereWithController:weSelf withImage:^(UIImage *image) {
                                model.B_image = image;
                                [tableView reloadData];
                            }];
                            
                        } else if (index == 1) {
                            [weSelf.photoManager openSinglePhotoWithController:weSelf withImage:^(UIImage *image) {
                                model.B_image = image;
                                [tableView reloadData];
                            }];
                        }
                    }];
                } else {
                    B_InputTextVC * input = [[B_InputTextVC alloc] init];
                    input.B_text = model.B_text;
                    input.B_doneAction = ^(NSString *string) {
                        model.B_text = string;
                        [tableView reloadData];
                    };
                    [weSelf pushNextViewController:input];
                    
                }
            }
        };
    }
    return _mainView;
}

- (XGPhotoManager *)photoManager {
    if (!_photoManager) {
        _photoManager = [[XGPhotoManager alloc] init];
    }
    return _photoManager;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
